package com.achunt.justtype

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import android.widget.EditText
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

import android.util.Log

class MainActivity : AppCompatActivity() {
    lateinit var fragment: JustType

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
            != PackageManager.PERMISSION_GRANTED
        ) {
            // Permission is not granted
            checkPermission(Manifest.permission.READ_CONTACTS, 1)
        }
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.BIND_ACCESSIBILITY_SERVICE)
            != PackageManager.PERMISSION_GRANTED
        ) {
            // Permission is not granted
            checkPermission(Manifest.permission.BIND_ACCESSIBILITY_SERVICE, 1)
        }

        if (savedInstanceState == null) {
            fragment = JustType()
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit()
        }
    }
    private fun checkPermission(permission: String, requestCode: Int) {
        // Checking if permission is not granted
        if (ContextCompat.checkSelfPermission(
                this@MainActivity,
                permission
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT)
                .show()
        } else {
            ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode)
        }
    }
    override fun onUserLeaveHint() {
        super.onUserLeaveHint()
    }
     override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
    }
    override fun onStop() {
        super.onStop()
    }
}
